package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.service.message.IdentityVerificationService;
import com.SmartCare.service.ImageService;
import com.SmartCare.service.role.UserRelationshipService;
import com.SmartCare.service.role.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping({"/client","/avatar"})
@Api(tags = "C端用户相关接口")
@Slf4j
public class ClientUserController {
    @Resource
    private UserService userService;
    @Resource
    private UserRelationshipService userRelationshipService;
    @Resource
    private IdentityVerificationService identityVerificationService;

    @Resource
    ImageService imageService;

    @Value("${files.upload.path}/iconImg/")
    private String fileUploadPath;

    /**
     * 获取getOpenIdAndSessionKey
     */
    @GetMapping("/getOpenId")
    @ApiOperation("获取openid")
    public Result getOpenIdAndSessionKey(String code){
        return  Result.success(userService.getOpenIdAndSessionKey(code));
    }

    /**
     * 获取用户手机号
     * @param code  动态令牌。可通过动态令牌换取用户手机号
     *              注：getPhoneNumber 返回的 code 与 wx.login 的 code 作用是不一样的，不能混用
     */
    @ApiOperation("获取用户手机号")
    @GetMapping("/getPhoneNumber")
    public Result getPhoneNumber(String code){
        return userService.getPhoneNumber(code);
    }


    /**
     * 微信登录
     * @param openid 小程序标识
     */
    @GetMapping("/login")
    @ApiOperation("微信登录")
    public Result login(HttpServletRequest request,String openid){
        //微信登录
        return userService.wxLogin(request,openid);
    }

    /**
     * 家属个人信息查询
     */
    @GetMapping("/familyInfo")
    public Result getFamilyInfo(Long userId){
        return userService.getFamilyInfo(userId);
    }
    @GetMapping("/getElderIds")
    public Result getElderIds(Long userId) {
        return Result.success(userRelationshipService.getElderIds(userId));
    }


    /**
     * 添加绑定老人信息
     */
    @GetMapping("/family/add")
    public Result AddFamilyInfo(Long elderlyId,
                                Long familyId,
                                String relationship){
        return userRelationshipService.addFamilyInfo(elderlyId,familyId,relationship);
    }

    //护工账户登录
    @GetMapping("/login_user")
    @Operation(summary = "护工登录", description = "用户账户进行登录")
    public Result login(@Parameter(description = "账户", required = true) @RequestParam String account,
                        @Parameter(description = "密码", required = true) @RequestParam String password,
                        @Parameter(description = "养老院名称", required = true) @RequestParam String careHomeName,
                        HttpServletRequest request) {

        return userService.userLogin(request,account, password,careHomeName);
    }

    /**
     * 游客注册
     */
    @GetMapping("/register")
    public Result register(String account,String password,String careHomeName,Integer identity) {
        return userService.register(account,password,careHomeName,identity);
    }

    /**
     * 绑定游客的养老院和身份信息
     */
    @GetMapping("/bindInfo")
    public Result updateUserInfo(Long userId,Integer identity,String careHomeName){
        return identityVerificationService.updateUserInfo(userId,identity,careHomeName);
    }
    //添加头像图片
    @PostMapping("/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "avatar/download");
    }
    //下载头像图片
    @GetMapping("/download/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    @GetMapping("/setMsg")
    public Result updateNameAndIcon(Long userId,
                                    @RequestParam(required = false) String avatarUrl,
                                    @RequestParam(required = false) String nickName){
        return userService.updateNameAndIcon(userId,avatarUrl,nickName);
    }

}
